<?php
/**
 * OA应用管理
 *
 * PHP version 5
 *
 * @package   YuDian
 * @author    lw
 * @copyright 2016 Shangyuanruige Inc.
 */
namespace app\controllers;

use app\models\app\Apps;
use app\models\app\Schoolapps;
use common\models\User;
use Yii;
use yii\data\Pagination;
use app\components\NoCsrfBehavior;

class AppsController extends CommonController
{
    public $navTabId;

    public function __construct($id, $module, $config = [])
    {
        $this->id = $id;
        $this->module = $module;

        parent::__construct($config);

        $this->navTabId = "appsindex";

    }

    /**
     * behaviors
     *
     * @author Fee
     */
    public function behaviors()
    {
        return [
            'csrf' => [
                'class' => NoCsrfBehavior::className(),
                'controller' => $this,
                'actions' => [
                    'index','open','close'
                ]
            ]
        ];
    }

    /**
     * 显示列表页面
     *
     * @return html
     * @author Fee
     */
    public function actionIndex() {
    	$this->checkAuth($this->id, $this->action->id);
        $order_arr['open_state'] = SORT_DESC;
        $order_arr['app_id'] = SORT_DESC;
        $where['open_state'] = [0,1];
        if (!empty($_POST['orderField'])) {
            $orderField = $_POST['orderField'];

            if (isset ( $_POST['orderDirection'] )) {
                $orderDirection = $_POST['orderDirection'] == 'asc' ? SORT_ASC : SORT_DESC;
            } else {
                $orderDirection = SORT_DESC;
            }

            $order_arr[$orderField] = $orderDirection;
        } else {
            $orderField = "";
            $orderDirection = "";
        }

        if (!empty($_POST['numPerPage'])){
            $numPerPage = $_POST['numPerPage'];
        } else {
            $numPerPage = Yii::$app->params['oa_page_num'];
        }


        $query = Apps::find()->where($where);
        $count = $query->count();

        $pages = new Pagination(['totalCount' => $count, 'pageSize' => $numPerPage]);
        $list = $query->orderBy($order_arr)
                        ->offset($pages->offset)
                        ->limit($pages->limit)
                        ->select('*')
                        ->with('school')
                        ->asArray()
                        ->all();
                        
        return $this->render('index',[
            'list' => $list,
            'orderDirection' => $orderDirection,
            'orderField'     => $orderField,
            'totalCount'     => $count,
            'filters'        => $_POST,
            'numPerPage'     => $pages->limit,
            'currentPage'    => !empty($_POST['p'])?$_POST['p']:1,
        ]);
    }

    /**
     * 详情
     * @return string
     */
    public function actionView()
    {
        $id =$_GET['id'];
        return $this->render('view', [
            'model' => $this->getModel($id),
            'state_list' => $this->getState(),
            'type_list' => $this->getType(),
            'bulid_type_list' => $this->getBulidtype(),
        ]);
    }

    /**
     * 获取需要传递到界面的额外数据
     *
     * @return html
     * @author Fee
     */
    public function getPageData($id=0)
    {
        return [
            'model' => $this->getModel($id),
            'state_list' => $this->getState(),
            'type_list' => $this->getType(),
            'bulid_type_list' => $this->getBulidtype(),
        ];
    }

    /**
     * 获取数据
     *
     * @return obj
     * @author Fee
     */
    public function getModel($id=0)
    {
        if($id == 0) {
            return new Apps();
        }

        if (($model = Apps::findOne($id)) !== null) {
            return $model;
        }

        return $this->ajaxReturn(300,Yii::t('app', 'The record does not exist. Please try again'),$this->navTabId, 1);
    }

    /**
     * 获取应用类型
     * @return array
     */
    public function getType()
    {
        return [
            '1'=>Yii::t('app/apps', 'System'),
            '2'=>Yii::t('app/apps', 'ThirdParty'),
        ];
    }

    /**
     * 获取开启状态
     * @return array
     */
    public function getState()
    {
        return [
            '1'=>Yii::t('app/apps', 'Open'),
            '0'=>Yii::t('app/apps', 'Close'),
        ];
    }

    /**
     * 获取应用类型
     * @return array
     */
    public function getBulidtype()
    {
        return [
            '1'=>Yii::t('app/apps', 'Native'),
            '2'=>Yii::t('app/apps', 'H5'),
        ];
    }

    /**
     * 开启
     */
    public function actionOpen()
    {
        $model = new Schoolapps();
        $id = trim($_GET['id']);
        $school_id = $_SESSION['school_id'];
        $data = array();
        $data['app_id'] = $id;
        $data['school_id'] = $school_id;
        $result = $model->insertInfo($data);
        if ($result) {
            $this->ajaxReturn(200, Yii::t('app/apps', 'OpenSuccess'));
        } else {
            $this->ajaxReturn(300, Yii::t('app/apps', 'OpenFail'));
        }
    }

    /**
     * 关闭
     */
    public function actionClose()
    {
        $model = new Schoolapps();
        $id = trim($_GET['id']);
        $school_id = $_SESSION['school_id'];
        $cond = array();
        $cond['app_id'] = $id;
        $cond['school_id'] = $school_id;
        $result = $model->deleteAll($cond);
        if ($result) {
            $this->ajaxReturn(200, Yii::t('app/apps', 'CloseSuccess'));
        } else {
            $this->ajaxReturn(300, Yii::t('app/apps', 'CloseFail'));
        }
    }
}